<?php $this->Html->addCrumb('Product Listings','/products'); ?>
<?php $this->Html->addCrumb('Pending Products'); ?>
<div class="products">
	<div class="search-title"><?php __('You are in:');?> Pending Products. Go to  <?php
		$options=array('unsold'=>'Unsold Products','active'=>'Active Products','disapproved'=>'Disapproved Products');
		echo $this->Form->select('action',$options,null,array('empty'=>'--Select--','onchange' => 'redirectaction(this)'));
		?></div>
     <?php echo $this->Form->create('Product', array('action' => 'pending')); ?>
<br /><input type="text" name="data[keywords]" value="Enter keywords or item number"  onblur="if(this.value=='') this.value='Enter keywords or item number'" onfocus="if(this.value=='Enter keywords or item number') this.value='';" size="50" />
<select name="data[search_by]">
<option value="">Search BY</option>
<option value="item_title">Item Title</option>
<option value="item_number">Item Number</option>
</select>
<input type="submit" name="searchSubmit" value="Search" />
</form>
	 
           <input type="button" class="add_selected" value="Add Note to Selected" name="Add Note to Selected" />&nbsp;
            <input type="button" class="send_selected" value="Send Selected Unsold" name="Send Selected Unsold" />&nbsp;
     <br><br>
        <table cellpadding="0" cellspacing="0" width="100%" class="tablesorter">
	<thead>
          <tr>
            <th><input type="checkbox" id="select_all" /></th>
            <th>&nbsp;</th>
            <th><?php echo $this->Paginator->sort('item #','id');?></th>
            <th><?php echo $this->Paginator->sort('Products','products_title');?></th>
            <th><?php echo $this->Paginator->sort('Price','products_price');?></th>
            <th><?php echo $this->Paginator->sort('Total Hits','products_views');?></th>
            <th class="actions"><?php __('Actions');?></th>
	</tr>
        </thead>
	<?php
	if(!empty ($products)){
	$i = 0;
	foreach ($products as $product):
		$class = null;
		if ($i++ % 2 == 0) {
			//$class = ' class="altrow"';
		}
	?>
        <tbody>
	<tr<?php echo $class;?> >
		<td class="pl_borderbottom"><input type="checkbox" id="products_id" value="<?php echo $product['Product']['id']; ?>" /></td>
		<td class="pl_borderbottom"> <?php
				
			   echo $html->image('thumbs/'.$thumbnail->render($product['Product']['products_thumbnailImage'],
                   array(
                        'path'=>'thumbs',
                        'width'=>140,
                        'height'=>140,
                        'quality'=>80
                        )
                    ),array("border"=>"0","title"=>$product['Product']['products_title'])
                );
                ?></td>
		<td align="center" class="pl_borderbottom"><?php echo $product['Product']['id']; ?></td>
		<td align="center" class="pl_borderbottom"><?php echo $this->Html->link(__($product['Product']['products_title'], true), array('action' => 'details/'.$product['Product']['id'])); ?></td>
		<td align="center" class="pl_borderbottom">$<?php echo $product['Product']['products_price']; ?></td>
		<td align="center" class="pl_borderbottom"><?php echo $product['Product']['products_views']; ?></td>
		<td class="actions pl_borderbottom" align="center">
                    <?php
		$options=array('sunsold'=>'Send to Unsold','add'=>'Add Note');
		echo $this->Form->select('action',$options,null,array('empty'=>'--Select--','id'=>$product['Product']['id'],'onchange' => 'redirectaction(this)'));
		?>		
		</td>
	</tr>
       <tr<?php echo $class;?> >
            <td colspan="7" id="disnote<?php echo $product['Product']['id']; ?>" class="clicktoedit">
        <?php
        if(!empty ($product['Note'])){
        ?>
        My Note:
    <?php
        $j=1;
        foreach($product['Note'] as $comment) {
        echo '<form method="post" id="inlineformcom'.$comment['id'].'"  name="inlineformcom" action="#"><p>'.$j.'.<label>'.$comment['note'];echo"</label><a href=\"javascript:void(0)\" id=".$comment['id'].">Edit</a></p></form>";
        $j++;
        }
        ?>
       <hr>
        <?php
        }
        ?> </td></tr>
        <tr style="display:none;" id="available<?php echo $product['Product']['id']; ?>">
         <td colspan="7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <form method="post" id="fcomment<?php echo $product['Product']['id']; ?>"  name="fcom" action="#">
             <input type="text" name="data[Note][note]" class="required" value="" id="comment<?php echo $product['Product']['id']; ?>" size="75"  style="background: none repeat scroll 0pt 0pt HoneyDew;"/>
             &nbsp;&nbsp;&nbsp;<input class="button" type="button" value="Save" onclick="return savenote(<?php echo $product['Product']['id']; ?>);" />
         &nbsp;&nbsp;&nbsp;<input class="button" type="button" value="Cancel" onclick="return entrycancel(<?php echo $product['Product']['id']; ?>);"/>
             </form>
             </td></tr>

<?php endforeach;
        }else{

          ?>
        <tr>
         <td colspan="7">
             No Pending Product Found.
          </td>
        </tr>
            <?php
        }
?>
        
	</tbody>
	</table>
<?php echo $this->Html->css('table_css');?>
<?php  echo $html->script(array("jquery.tablesorter.min.js","jquery.tablednd.js")); ?>
   <style type="text/css">
      table.tablesorter tbody td {
    border-bottom: 1px solid #CCCCCC;
    border-right: 0px solid #CCCCCC;
    padding: 0px;
    vertical-align: top;
    }
   </style>
   <script type="text/javascript">
            jQuery(document).ready(function(){
               var prevtext;
               var commentid;
               jQuery(".clicktoedit p a").live('click', function(){
               commentid=jQuery(this).attr("id");
               //alert(commentid);
                prevtext = jQuery(this).parent().find('label').text();
                jQuery(this).parent().find('label').replaceWith('<input type="text" name="data[Note][note]"  size="50"  class="takeData required" value="'+prevtext+'" /><button class="saver">Save</button><button class="canceler">Cancel</button></form>');
                jQuery(this).remove();
                //jQuery(this).parent().addClass('editMode');
                jQuery("td.clicktoedit").each(function (index,domEle) {
					  jQuery(domEle).removeClass("clicktoedit");
					  jQuery(domEle).addClass("clickToCancel");
            });
               });

               jQuery(".canceler").live("click", function(){
                   jQuery(this).parent().find('.takeData').replaceWith('<label>'+prevtext+'</label><a id="'+commentid+'" href="javascript:void(0)">Edit</a>');
                   jQuery(this).parent().find('.saver').remove();
                   //jQuery(this).parent().removeClass('editMode')
                   jQuery(this).parent().find('button').remove();
                    jQuery("td.clickToCancel").each(function (index,domEle) {
                        jQuery(domEle).removeClass("clickToCancel");
                        jQuery(domEle).addClass("clicktoedit");
                    });
               });

               jQuery(".saver").live("click", function(){
                   jQuery("td.clickToCancel").each(function (index,domEle) {
                        jQuery(domEle).removeClass("clickToCancel");
                        jQuery(domEle).addClass("clicktoedit");
                    });
                var note=jQuery(this).parent().find(".takeData").val();
                if(note==''){
                return false;
                }
      data = jQuery("#inlineformcom"+commentid).serialize();
      jQuery(this).parent().find('.takeData').replaceWith('<label>'+jQuery(this).parent().find(".takeData").val()+'</label><a id="'+commentid+'" href="javascript:void(0)">Edit</a>');
                jQuery(this).parent().find('button').remove();
      jQuery.ajax({
                type: 'POST',
                url: baseurl+"products/updatenote/"+commentid,
                data:data,
                success: function(){

                }
	});
               });
            });
        </script>
        <div  style="margin-top:3px;">
           <input type="button" class="add_selected" value="Add Note to Selected" name="Add Note to Selected" />&nbsp;
           <input type="button" class="send_selected" value="Send Selected Unsold" name="Send Selected Unsold" />&nbsp;

        </div>
	<p>
	<?php
	echo $this->Paginator->counter(array(
	'format' => __('Page %page% of %pages%', true)
	));
	?>	</p>

	<div class="paging">
		<?php echo $this->Paginator->prev('< ', array(), null, array('class'=>'disabled'));?>
	 | 	<?php echo $this->Paginator->numbers();?> |
		<?php echo $this->Paginator->next(' >', array(), null, array('class' => 'disabled'));?>
	</div>
</div>
<style type="text/css">
th,th a{font-size:14px; font-weight:700; text-decoration:none;color:black;}
</style>
<?php echo $html->script(array("jquery.validate.js")); ?>
<script type="text/javascript">
   
jQuery("#select_all").click(function(){
				var checked_status = this.checked;
				jQuery("input[id=products_id]").each(function()
				{
                                 //var mid=jQuery(this).val();
                                 this.checked = checked_status;    				 
				});
});

jQuery(".add_selected").click(function(){
				//var checked_status = this.checked;
				jQuery("input[id=products_id]").each(function()
				{
                                 var mid=jQuery(this).val();
                                var checked_status = this.checked;
                                //this.checked = checked_status;
    				 if(checked_status==true){
                                         jQuery('#available'+mid).show();
                                         }
				});
});

        jQuery(".send_selected").click(function(){
            jQuery("input[id=products_id]").each(function()
            {
            var mid=jQuery(this).val();
            var checked_status = this.checked;
            if(checked_status==true){
                jQuery.ajax({
                    type: 'GET',
                    url : baseurl+'products/unsoldselected/'+mid,
                dataType: 'html',
                success: function(data){
                    window.location.reload();
                }
            });
        }
    });

});

function redirectaction(param){
        var mval=jQuery(param).val();
        var mid=jQuery(param).attr('id');
        ////////delete//////////
        if(mval=='end'){
            var r=confirm("Do you really want to delete the product?");
            if(r){
              window.location = baseurl+'products/delete/'+mid;
            }
          }
      ////////////////////////Edit//////////
        if(mval=='edit'){
           window.location = baseurl+'products/edit/'+mid;
            }
    ////////////////////////add note//////////
        if(mval=='add'){
           //window.location = baseurl+'products/edit/'+mid;
            jQuery('#available'+mid).show();
            }
              ////////////////////////unsold//////////
        if(mval=='unsold'){
           window.location = baseurl+'products/unsold/';
            //jQuery('#available'+mid).show();
            }

            ////////////////////////sunsold//////////
        if(mval=='sunsold'){
                jQuery.ajax({
                    type: 'GET',
                    url : "<?php echo Configure::read('base_url'); ?>products/unsoldselected/"+mid,
                    dataType: 'html',
                    success: function(data){
                            window.location.reload();
                    }
                });
            }
               ////////////////////////active//////////
        if(mval=='active'){
           window.location = baseurl+'products/index';            
            }

////////////////////////disapproved//////////
        if(mval=='disapproved'){
           window.location = baseurl+'products/disapproved/';            
            }
    }

 function entrycancel(id){
      jQuery('#available'+id).hide();
 }

 function savenote(id){      
      var note=jQuery('#comment'+id).val();      
      if(note==''){
           return false;
      }
      jQuery('#available'+id).hide();
      data = jQuery("#fcomment"+id).serialize();
      jQuery.ajax({
                type: 'POST',
                url: baseurl+"products/addnote/"+id,
                data:data,
                success: function(html){
                       document.getElementById("disnote"+id).innerHTML = "";
                       //jQuery("#disnote"+id).append(html);
                        window.location.reload();
                }
	});
 }
</script>
<style type="text/css">

    table.tablesorter tbody tr.even td {
        border: none;
}
table.tablesorter {
    background-color: #fff;
}
</style>